<?php

namespace addons\TfFutures\backend\controllers;

use BaconQrCode\Common\Mode;
use Yii;
use yii\base\Model;
use yii\web\UploadedFile;

class UploadForm extends Model
{
    public $imageFile;
    public $icon;
    public $content;

    public function rules()
    {
        return [
            [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
            [['content'], 'required'],
            [['icon'], 'string'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'imageFile' => '图片',
            'icon' => '支付类型',
            'content' => '内容',
        ];
    }

    public function upload()
    {
        $path = $_SERVER['DOCUMENT_ROOT'] . Yii::getAlias('/attachment/images/pay/');
        if (!is_dir($path)) {
            mkdir($path);
        }
        var_dump($this->imageFile);
        //echo $href;
        $jud = $this->imageFile->saveAs($path . $this->imageFile->baseName . '.' . $this->imageFile->extension);
        //$jud = move_uploaded_file($this->imageFile->tempName, $path);
        if ($jud) {
            $server_path = $_SERVER['SERVER_NAME'] . Yii::getAlias('/attachment/images/pay/') . $this->imageFile->name;
            return 'http://' . $server_path;
        }
        return false;
    }


    //上传支付图片
    public function uploadIcon()
    {
        $path = $_SERVER['DOCUMENT_ROOT'] . Yii::getAlias('/attachment/images/payIcon/');
        if (!is_dir($path)) {
            mkdir($path);
        }
        var_dump($this->imageFile);
        //echo $href;
        $jud = $this->imageFile->saveAs($path . $this->imageFile->baseName . '.' . $this->imageFile->extension);
        //$jud = move_uploaded_file($this->imageFile->tempName, $path);
        if ($jud) {
            $server_path = $_SERVER['SERVER_NAME'] . Yii::getAlias('/attachment/images/payIcon/') . $this->imageFile->name;
            return 'http://' . $server_path;
        }
        return false;
    }

}